/**
 * Created by dell on 2018/12/7.
 */

const http = require('http');
const url = require('url');
const querystring = require('querystring');
const ss = require('./06.js');

//启动服务器
http.createServer((req,res)=>{
    //启动静态服务器
    //指定是www开头的文件
    if(req.url.startsWith('/www')){
        ss.staticServer(req,res,__dirname);
    }
    console.log(req.url);
    //启动动态服务器
    if(req.url.startsWith('/login')){
        if(req.method=='POST'){
            let pdata='';
            //req.on(data)指每次发送的数据
            req.on('data',(chunk)=>{
                pdata +=  chunk;
            });
            //req.on(end)数据发送完成
            req.on('end',()=>{
                let obj = querystring.parse(pdata);
                if(obj.username=='admin' && obj.password == '123'){
                    res.end('post success');
                }else{
                    res.end('post failure');
                }

            });
        }
        if(req.method=='GET'){
           let param = url.parse(req.url,true).query;
            if(param.username == 'admin' && param.password == '123'){
                res.end('get success');
            }else{
                res.end('get failure');
            }
        }
        
    }

}).listen(3000,()=>{
    console.log('running.....');
});


